/*
 * Created by JFormDesigner on Thu Jul 04 00:18:06 CST 2024
 */

package scct.ZhuZhouXing.ui;

import jdk.nashorn.internal.scripts.JO;
import scct.ZhuZhouXing.dao.StaffDao;
import scct.ZhuZhouXing.model.Staff;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * @author zr155
 */
public class DlgStaffUpdate extends JDialog {
    int id;

    public DlgStaffUpdate(Window owner, int id) {
        super(owner);
        this.id = id;
        initComponents();
        initView();
    }

    private void initView() {
        StaffDao staffDao = new StaffDao();
        Staff staff = staffDao.getUserById(this.id);
        txtName.setText(staff.getAusername());
        boxtAge.setValue(staff.getAge());
        boxPost.setVisible(true);
        boxPay.setVisible(true);
        if (staff.getGender().equals("男")) {
            radMan.setSelected(true);
        } else {
            radMan.setSelected(true);
        }

        textPhone.setText(staff.getPhone());
    }

    private void btnUpdate(ActionEvent e) {
        String ausername = txtName.getText();
        int age = Integer.parseInt(boxtAge.getModel().getValue().toString());
        String post = (String) boxPost.getSelectedItem();
        String gender = radMan.isSelected() ? "男" : "女";
        String salary = (String) boxPay.getSelectedItem();
        String phone = textPhone.getText();
        StaffDao staffDao = new StaffDao();
        if (ausername.isEmpty()){
            JOptionPane.showMessageDialog(this,"姓名不能为空！");
            return;
        }
        if (age<18||age>60){
            JOptionPane.showMessageDialog(this,"年龄范围为在18~60之间！");
            return;
        }
        if (phone.isEmpty()|| !phone.matches("^1[3-9]\\d{9}$")){
        JOptionPane.showMessageDialog(this,"电话号码格式不对");
        return;
        }
        if (staffDao.updateStaff(id, ausername, gender, age, post, phone, salary)) {
            JOptionPane.showMessageDialog(this, "修改成功");
            PaneStaffManager.getTable();
            this.dispose();
        } else {
            JOptionPane.showMessageDialog(this, "修改失败");

        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - zhur
        label1 = new JLabel();
        label2 = new JLabel();
        txtName = new JTextField();
        label8 = new JLabel();
        boxtAge = new JSpinner();
        label9 = new JLabel();
        boxPost = new JComboBox<>();
        label5 = new JLabel();
        boxPay = new JComboBox<>();
        label6 = new JLabel();
        label7 = new JLabel();
        textPhone = new JTextField();
        btnUpdate = new JButton();
        button2 = new JButton();
        radMan = new JRadioButton();
        radioButton2 = new JRadioButton();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u4fee\u6539\u5458\u5de5");
        label1.setFont(new Font("Microsoft JhengHei UI", label1.getFont().getStyle() | Font.BOLD, label1.getFont().getSize() + 13));
        contentPane.add(label1);
        label1.setBounds(240, 15, 160, 55);

        //---- label2 ----
        label2.setText("\u59d3\u540d");
        label2.setFont(label2.getFont().deriveFont(label2.getFont().getSize() + 6f));
        contentPane.add(label2);
        label2.setBounds(165, 70, 45, 29);
        contentPane.add(txtName);
        txtName.setBounds(250, 70, 165, 30);

        //---- label8 ----
        label8.setText("\u5e74\u9f84");
        label8.setFont(label8.getFont().deriveFont(label8.getFont().getSize() + 6f));
        contentPane.add(label8);
        label8.setBounds(165, 117, 45, 29);

        //---- boxtAge ----
        boxtAge.setModel(new SpinnerNumberModel((byte) 18, (byte) 18, (byte) 60, (byte) 1));
        contentPane.add(boxtAge);
        boxtAge.setBounds(250, 117, 165, 30);

        //---- label9 ----
        label9.setText("\u804c\u4f4d");
        label9.setFont(label9.getFont().deriveFont(label9.getFont().getSize() + 6f));
        contentPane.add(label9);
        label9.setBounds(165, 164, 45, 29);

        //---- boxPost ----
        boxPost.setModel(new DefaultComboBoxModel<>(new String[] {
            "\u5458\u5de5",
            "\u5c0f\u7ec4\u7ec4\u957f",
            "\u4e3b\u7ba1",
            "\u526f\u7ecf\u7406",
            "\u7ecf\u7406",
            "\u526f\u5e97\u957f",
            "\u5e97\u957f"
        }));
        contentPane.add(boxPost);
        boxPost.setBounds(250, 164, 165, 30);

        //---- label5 ----
        label5.setText("\u85aa\u8d44");
        label5.setFont(label5.getFont().deriveFont(label5.getFont().getSize() + 6f));
        contentPane.add(label5);
        label5.setBounds(165, 211, 45, 29);

        //---- boxPay ----
        boxPay.setModel(new DefaultComboBoxModel<>(new String[] {
            "4000",
            "4500",
            "6000",
            "6500",
            "8000",
            "8500"
        }));
        contentPane.add(boxPay);
        boxPay.setBounds(250, 211, 165, 30);

        //---- label6 ----
        label6.setText("\u6027\u522b");
        label6.setFont(label6.getFont().deriveFont(label6.getFont().getSize() + 6f));
        contentPane.add(label6);
        label6.setBounds(165, 258, 45, 29);

        //---- label7 ----
        label7.setText("\u7535\u8bdd");
        label7.setFont(label7.getFont().deriveFont(label7.getFont().getSize() + 6f));
        contentPane.add(label7);
        label7.setBounds(165, 305, 45, 29);
        contentPane.add(textPhone);
        textPhone.setBounds(250, 305, 165, 30);

        //---- btnUpdate ----
        btnUpdate.setText("\u4fee\u6539");
        btnUpdate.setFont(btnUpdate.getFont().deriveFont(btnUpdate.getFont().getSize() + 10f));
        btnUpdate.addActionListener(e -> btnUpdate(e));
        contentPane.add(btnUpdate);
        btnUpdate.setBounds(155, 365, 83, 37);

        //---- button2 ----
        button2.setText("\u53d6\u6d88");
        button2.setFont(button2.getFont().deriveFont(button2.getFont().getSize() + 10f));
        contentPane.add(button2);
        button2.setBounds(355, 365, 83, 37);

        //---- radMan ----
        radMan.setText("\u7537");
        radMan.setSelected(true);
        contentPane.add(radMan);
        radMan.setBounds(new Rectangle(new Point(255, 265), radMan.getPreferredSize()));

        //---- radioButton2 ----
        radioButton2.setText("\u5973");
        contentPane.add(radioButton2);
        radioButton2.setBounds(330, 265, 47, 21);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(560, 560);
        setLocationRelativeTo(getOwner());

        //---- buttonGroup1 ----
        ButtonGroup buttonGroup1 = new ButtonGroup();
        buttonGroup1.add(radMan);
        buttonGroup1.add(radioButton2);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - zhur
    private JLabel label1;
    private JLabel label2;
    private JTextField txtName;
    private JLabel label8;
    private JSpinner boxtAge;
    private JLabel label9;
    private JComboBox<String> boxPost;
    private JLabel label5;
    private JComboBox<String> boxPay;
    private JLabel label6;
    private JLabel label7;
    private JTextField textPhone;
    private JButton btnUpdate;
    private JButton button2;
    private JRadioButton radMan;
    private JRadioButton radioButton2;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
